package com.younion.action.consumer.impl;

import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.List;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import com.younion.action.consumer.AbstractRequestConsumer;
import com.younion.action.interactive.mobile.ApiConstant;
import com.younion.action.interactive.mobile.ResultObject;
import com.younion.business.IMemberCardService;
import com.younion.common.constants.SysConstants;
import com.younion.model.MyKabaoListResult;
import com.younion.util.HttpRequestUtils;
import com.younion.util.JsonUtils;
import com.younion.util.config.ConfigHelper;
import com.younion.vo.API_MemberCardVO;
import com.younion.vo.MemberCardVO;

public class MyCardListGetConsumer extends AbstractRequestConsumer {

	private IMemberCardService memberCardService;

	@Override
	public String execute(HttpServletRequest request,
			HttpServletResponse response) throws Exception {
		return getMyCardList(request, response);
	}

	public String getMyCardList(HttpServletRequest request,
			HttpServletResponse response) {
		String user_token = request.getParameter("user_token");
		List<MyKabaoListResult> myKabaoResultResultList = new ArrayList<MyKabaoListResult>();
		List<API_MemberCardVO> api_MemberCardVOList = new ArrayList<API_MemberCardVO>();
		List<MemberCardVO> memberCardVOList = null;
		ResultObject resultObject = new ResultObject();
		try {
			api_MemberCardVOList = memberCardService
					.getMyCardByUserToken(user_token);
			this.processCardEntityData(api_MemberCardVOList,
					myKabaoResultResultList);
			resultObject.setResult(myKabaoResultResultList);
			resultObject.setStatus(ApiConstant.SUCCESS_STATUS);

		} catch (Exception e) {
			resultObject.setStatus(ApiConstant.FALSE_STATUS);
			resultObject.setMsg(ApiConstant.PUBLIC_EXCEPTION);
			e.printStackTrace();
		}

		HttpRequestUtils.printContent(JsonUtils.parseObj2Json(resultObject),
				response);
		return null;
	}

	private void processCardEntityData(
			List<API_MemberCardVO> api_MemberCardVOList,
			List<MyKabaoListResult> myKabaoResultResultList) {
		String img_url_pre = ConfigHelper.fastGetConfig("interface",
				"img_url_pre", "http://localhost:8080/younonmember/");
		SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
		for (API_MemberCardVO api_memberCardVO : api_MemberCardVOList) {
			MyKabaoListResult myKabaoListResult = new MyKabaoListResult();
			myKabaoListResult.setCard_id(api_memberCardVO.getId().toString());
			myKabaoListResult.setCard_entity_id(api_memberCardVO.getApi_CardEntityVO().getId().toString());
			myKabaoListResult.setCard_name(api_memberCardVO
					.getApi_CardEntityVO().getCardName());
			myKabaoListResult.setCard_no(api_memberCardVO.getSchemeCode());
			myKabaoListResult.setShop(api_memberCardVO.getApi_CardEntityVO()
					.getCustomerVO().getShops());
			myKabaoListResult.setTime_from(sdf.format(api_memberCardVO
					.getApi_CardEntityVO().getExpiryDateFrom()));
			myKabaoListResult.setTime_to(sdf.format(api_memberCardVO
					.getApi_CardEntityVO().getExpiryDateTo()));
			myKabaoListResult.setActivatedPic(img_url_pre
					+ SysConstants.CARD_IMG_URL + "/"
					+ api_memberCardVO.getApi_CardEntityVO().getId()+ "/"
					+api_memberCardVO.getApi_CardEntityVO().getActivatedPic());
			myKabaoListResult.setNonactivatedPic(img_url_pre
					+ SysConstants.CARD_IMG_URL
					+ "/"
					+ api_memberCardVO.getApi_CardEntityVO().getId()+ "/"
					+ api_memberCardVO.getApi_CardEntityVO()
							.getNonactivatedPic());
			myKabaoListResult.setApplyWay(api_memberCardVO.getApi_CardEntityVO().getApplyWay().toString());
			myKabaoListResult.setNearby("1000");
			myKabaoListResult.setCategory_id(api_memberCardVO
					.getApi_CardEntityVO().getCustomerVO().getCustomerKindId()
					.toString());
			String countUrl = api_memberCardVO.getApi_CardEntityVO().getCustomerVO().getCountUrl();
			String balanceUrl = api_memberCardVO.getApi_CardEntityVO().getCustomerVO().getBalanceUrl();
			myKabaoListResult.setCountUrl(countUrl!=null?countUrl+"card_id="+api_memberCardVO.getId():"");
			myKabaoListResult.setBalanceUrl(balanceUrl!=null?balanceUrl+"card_id="+api_memberCardVO.getId():"");
			myKabaoResultResultList.add(myKabaoListResult);
		}
	}

	public void setMemberCardService(IMemberCardService memberCardService) {
		this.memberCardService = memberCardService;
	}
}
